package controllers

import (
	"fmt"
	"log"

	"boomlive.com/boomlive-common/configs"
	"boomlive.com/boomlive-common/constants"
	"boomlive.com/boomlive-common/utils"
)

type TestController struct {
	BaseController
}

func (c *TestController) Index() {
	c.CodeResult(constants.SUCCESS, nil)
}

// @router /joyplay/userinfo [get]
//func (c *TestController) TestGetDB() {
//
//	// 直接使用 GetDB 获取连接
//	db, err := utils.GetDB(configs.BoomliveDBAliasName)
//	if err != nil {
//		log.Fatalf("获取数据库失败: %v", err)
//	}
//	var user models.Users
//	err = db.Get(&user, "SELECT user_id, gender, sub_region_id, status, update_time, nick, head_img, age, free_chat_count FROM users WHERE user_id = ?", 190308)
//	if err != nil {
//		log.Fatalf("查询用户失败: %v", err)
//	}
//
//	fmt.Printf("查询结果：%+v\n", user)
//
//	c.CodeResult(constants.SUCCESS, nil)
//	return
//}
//
//// @router /joyplay/Submitflow [post]
//func (c *TestController) TestORM() {
//
//	userInfo, err := models.GetUsersByUserId(190308)
//	if err != nil {
//		c.Error("GetUsersByUserId(%v): %v", 190308, err)
//		c.CodeResult(constants.ERR_SYS, userInfo)
//		return
//	}
//
//	c.CodeResult(constants.SUCCESS, userInfo)
//	return
//}

type UserBaseInfo struct {
	PhotoNum     string `json:"photo_num" mapstructure:"photo_num" db:"photo_num"`
	NickNm       string `json:"nick_nm" mapstructure:"nick_nm" db:"nick_nm"`
	UserSex      bool   `json:"user_sex" mapstructure:"user_sex" db:"user_sex"`
	UserInterest int    `json:"-" mapstructure:"user_interest" db:"user_interest"`
}

func (c *TestController) TestMSSQL() {

	// 获取连接
	db, err := utils.GetDB(configs.UnicomDBAliasName)
	if err != nil {
		log.Fatalf("获取数据库连接失败: %v", err)
		c.Result(constants.ERR_SYS, nil)
	}

	// 查询：调用存储过程
	var result []UserBaseInfo
	query := `EXEC pr_getUserBaseInfo @uid=:uid`

	rows, err := db.NamedQuery(query, map[string]interface{}{
		"uid": 708376328,
	})
	if err != nil {
		log.Fatalf("执行存储过程失败: %v", err)
		c.Result(constants.ERR_SYS, nil)
	}
	defer rows.Close()

	for rows.Next() {
		var info UserBaseInfo
		if err = rows.StructScan(&info); err != nil {
			log.Fatalf("结果解析失败: %v", err)
			c.Result(constants.ERR_SYS, nil)
		}
		result = append(result, info)
	}

	// 打印结果
	for _, user := range result {
		fmt.Printf("用户信息：%+v\n", user)
	}

	c.Result(constants.SUCCESS, nil)
	return
}

// TestGetUserToken 测试获取用户token
func (c *TestController) TestRedis() {

	Redisserver.Set("test", "test", 0)
	value, err := Redisserver.Get("test")

	if err != nil {
		c.Error("Redisserver.Get: %v", err)
		c.CodeResult(constants.ERR_SYS, nil)
		return
	}
	c.Info("Redisserver.Get: %v", value)
	fmt.Println(value)

	c.CodeResult(constants.SUCCESS, value)
	return
}
